<script setup>
import { ref } from 'vue'
import { ElMessage, ElForm, ElFormItem, ElInput, ElSelect, ElOption, ElButton } from 'element-plus'
import { addFeedbackApi } from '@/api/feedback'

// 表单数据
const formData = ref({
  type: '0', // 0-投诉，1-建议
  title: '',
  content: '',
  userPhone: ''
})

// 表单验证规则
const rules = {
  type: [
    { required: true, message: '请选择类型', trigger: 'change' }
  ],
  title: [
    { required: true, message: '请输入标题', trigger: 'blur' },
    { min: 2, max: 50, message: '标题长度在 2 到 50 个字符', trigger: 'blur' }
  ],
  content: [
    { required: true, message: '请输入内容', trigger: 'blur' },
    { min: 10, max: 1000, message: '内容长度在 10 到 1000 个字符', trigger: 'blur' }
  ],
  userPhone: [
    { required: true, message: '请输入联系电话', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ]
}

// 提交状态
const submitLoading = ref(false)

// 表单引用
const formRef = ref(null)

// 提交表单
const submitForm = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    submitLoading.value = true
    
    // 获取当前登录用户信息
    const loginUser = JSON.parse(localStorage.getItem('loginUser'))
    if (!loginUser) {
      ElMessage.error('请先登录')
      return
    }
    
    const submitData = {
      ...formData.value,
      userId: loginUser.id,
      userName: loginUser.name
    }
    
    const res = await addFeedbackApi(submitData)
    if (res.code) {
      ElMessage.success('提交成功，感谢您的反馈！')
      // 重置表单
      resetForm()
    } else {
      ElMessage.error(res.msg || '提交失败，请稍后重试')
    }
  } catch (error) {
    if (error.name !== 'ValidationError') {
      ElMessage.error('提交异常：' + error.message)
    }
  } finally {
    submitLoading.value = false
  }
}

// 重置表单
const resetForm = () => {
  if (formRef.value) {
    formRef.value.resetFields()
  }
  formData.value = {
    type: '0',
    title: '',
    content: '',
    userPhone: ''
  }
}

// 取消提交
const cancelSubmit = () => {
  resetForm()
  ElMessage.info('已取消提交')
}
</script>

<template>
  <div class="feedback-container">
    <div class="page-header">
      <h2>投诉建议</h2>
      <p>您的反馈对我们很重要，我们会认真对待每一条意见</p>
    </div>
    
    <el-card class="card-style">
      <el-form
        ref="formRef"
        :model="formData"
        :rules="rules"
        label-width="100px"
        class="feedback-form"
      >
        <el-form-item label="类型" prop="type">
          <el-select v-model="formData.type" placeholder="请选择类型">
            <el-option label="投诉" value="0" />
            <el-option label="建议" value="1" />
          </el-select>
        </el-form-item>
        
        <el-form-item label="标题" prop="title">
          <el-input v-model="formData.title" placeholder="请输入标题" maxlength="50" show-word-limit />
        </el-form-item>
        
        <el-form-item label="联系电话" prop="userPhone">
          <el-input v-model="formData.userPhone" placeholder="请输入手机号码" maxlength="11" />
        </el-form-item>
        
        <el-form-item label="详细内容" prop="content">
          <el-input
            v-model="formData.content"
            type="textarea"
            :rows="3"
            placeholder="请详细描述您的问题或建议，不少于10个字符"
            maxlength="500"
            show-word-limit
          />
        </el-form-item>
        
        <el-form-item>
          <div class="form-tips">
            <p><i class="el-icon-info"></i> 温馨提示：</p>
            <p>1. 请提供真实有效的联系电话，以便我们及时回复您</p>
            <p>2. 我们会在1-3个工作日内处理您的反馈</p>
            <p>3. 感谢您对我们服务的支持和监督</p>
          </div>
        </el-form-item>
        
        <el-form-item>
          <div class="form-actions">
            <el-button type="primary" @click="submitForm" :loading="submitLoading" :disabled="submitLoading">
              提交反馈
            </el-button>
            <el-button @click="cancelSubmit" :disabled="submitLoading">
              取消
            </el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped>
.feedback-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  min-height: 100vh;
  background-color: #f5f7fa;
}

.page-header {
  margin-bottom: 20px;
  color: #303133;
}

.page-header h2 {
  margin: 0 0 10px 0;
  font-size: 24px;
  font-weight: 500;
}

.page-header p {
  margin: 0;
  color: #666;
  font-size: 16px;
}

.card-style {
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  border: none;
}

.card-style .el-card__body {
  padding: 20px;
}

.feedback-form {
  margin: 0;
}

.feedback-form .el-form-item {
  margin-bottom: 20px;
}

.form-tips {
  background-color: #f0f9eb;
  border: 1px solid #e1f3d8;
  border-radius: 4px;
  padding: 15px;
  margin-bottom: 20px;
}

.form-tips p {
  margin: 5px 0;
  color: #67c23a;
  font-size: 14px;
  line-height: 1.6;
}

.form-actions {
  display: flex;
  gap: 15px;
  justify-content: flex-start;
  margin-top: 20px;
}

.el-button {
  margin-right: 10px;
}

/* 输入框和选择器样式优化 */
.el-input,
.el-select {
  width: 100%;
}

:deep(.el-input__inner) {
  border-radius: 4px;
}

:deep(.el-textarea__inner) {
  border-radius: 4px;
  min-height: 60px;
}

:deep(.el-form-item__error) {
  padding-top: 4px;
}
</style>